package edu.gatech.oad;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class CreateNewUser extends Activity {
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        // Set View to register.xml
	        setContentView(R.layout.create_new_user);

	    	final EditText location= (EditText) findViewById(R.id.editLocation);	    	
	    	final EditText firstName= (EditText) findViewById(R.id.firstName);	    	
	    	final EditText lastName= (EditText) findViewById(R.id.LastName);

	    	// email - handled as @Username in accordance to DataBaseHelper
	    	// doesn't remove @email - should we do this? or should we add
	    	// another separate username EditText field?
	    	final EditText eMail = (EditText) findViewById(R.id.Email);
	    	final EditText initialPass= (EditText) findViewById(R.id.Password);
	    	final EditText confirmPass= (EditText) findViewById(R.id.ConfirmPassword);
	    	final CheckBox checkAdmin = (CheckBox) findViewById(R.id.adminCheck);
	    	final Button confirmRegistration = (Button) findViewById(R.id.Finish);
	    	
	        
	        // Listening to @confirmRegistration button to check data - 
	        // if legal data pass to db and send to the Main Activity - 
	        // if not legal throw appropriate errors and notify the user
	        // (error messages not implemented yet - currently accepts
	        // nulls etc - because I set the input text maybe it innately
	        // does this - not sure yet)
	        
	        confirmRegistration.setOnClickListener(new View.OnClickListener() {
	        	public void onClick(View v) {
	        		
	        		// getting the data, casting to type String and setting to final String vars
	        		final String _FIRST_NAME = firstName.getText().toString();
	        		final String _LAST_NAME = lastName.getText().toString();
	        		final String _EMAIL = eMail.getText().toString();
	        		final String _INITIAL_PASS = initialPass.getText().toString();
	        		final String _CONFIRM_PASS = confirmPass.getText().toString();
	        		
	        		if(!_INITIAL_PASS.equals(_CONFIRM_PASS))
	        		{
	        			confirmPass.setError(getString(R.string.error_mismatch));
	        		}
	        		String isAdmin = "Regular";
	        		if(checkAdmin.hasSelection()){
	        			isAdmin = "Admin";
	        			System.out.println("Admin Granted");
	        		}
	        		
		        	DataBaseHelper db = new DataBaseHelper(getApplicationContext());
		        	try {
		        		db.createDataBase();
		        		db.openDataBase();
		        	} catch(Exception sqle) {
		        		throw new Error("Unable to open database");
		        	}
		        	
		        	// additional errors/exception? 
		        	// such as checking if intialPass.equals(confirmPass)
		        	// @eMail = @Username
		        	// account @AccountStatus is set to "Active"
		        	// account @Type is set to "User"
		        	db.registerUser(_EMAIL, _CONFIRM_PASS, _FIRST_NAME, _LAST_NAME, "Active", isAdmin);
		        	System.out.println("New User Created");
		        	// send authentication email?
		        	
		        	// login in acc and send to main or send back to login
		        	
		        	// for now send back to login w/ no confirm email
		        	
		        	
		        	// Switching to previous menu
		        	finish();
	                
	        	}
	        });
	      
}
}

